﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace neside3.func_edit
{
    /// <summary>
    /// dialog_newproject.xaml 的交互逻辑
    /// </summary>
    public partial class dialog_newproject : Window
    {
        public string ProjectPath
        {
            get;
            private set;
        }
        public dialog_newproject()
        {
            InitializeComponent();
        }
        class TemplateItem
        {
            public string folder;
            public IDE.ProjOption option;
            public override string ToString()
            {
                return "Project Type:" + option.buildtype;
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ProjectPath = null;
            var loc = this.GetType().Assembly.Location;
            var path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(loc), "plugin/build_template");
            try
            {
                var temps = System.IO.Directory.GetFiles(path, "neside.json", System.IO.SearchOption.AllDirectories);
                foreach (var f in temps)
                {
                    try
                    {
                        TemplateItem item = new TemplateItem();
                        item.option = IDE.ProjOption.FromFile(f);
                        item.folder = System.IO.Path.GetDirectoryName(f);
                        projectTypes.Items.Add(item);
                    }
                    catch
                    {

                    }
                }
                if (projectTypes.Items.Count > 0)
                    projectTypes.SelectedIndex = 0;
            }
            catch
            {

            }
        }

        private void projectTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            projectDesc.Text = "";
            TemplateItem item = projectTypes.SelectedItem as TemplateItem;
            if (item != null)
                projectDesc.Text = item.option.description;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {//project path peek
            System.Windows.Forms.FolderBrowserDialog m_Dialog = new System.Windows.Forms.FolderBrowserDialog();
            m_Dialog.Description = "请选择一个空目录用于创建项目/Please select a empty folder for create project.";
            var result = m_Dialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                txt_projectPath.Text = m_Dialog.SelectedPath;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {//create project;
            CreateProject();
        }

        private void CreateProject()
        {
            ProjectPath = txt_projectPath.Text;
            try
            {
                if (System.IO.Directory.Exists(ProjectPath) == false)
                {
                    System.IO.Directory.CreateDirectory(ProjectPath);
                }
                var files = System.IO.Directory.GetFiles(ProjectPath, "*.*", System.IO.SearchOption.AllDirectories);
                if (files != null && files.Length > 0)
                {
                    MessageBox.Show("错误:目录不为空/Error:need an empty folder here.");
                    this.DialogResult = false;
                    return;
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("错误/Error:" + err.Message);
                this.DialogResult = false;
                return;
            }

            //copy all
            TemplateItem item = projectTypes.SelectedItem as TemplateItem;
            var sourcepath = new System.IO.DirectoryInfo(item.folder);
            CopyFolder(sourcepath, ProjectPath);
            this.DialogResult = true;
            return;
        }

        void CopyFolder(System.IO.DirectoryInfo source, string dest)
        {
            foreach (var f in source.GetFiles())
            {
                f.CopyTo(System.IO.Path.Combine(dest, f.Name));
            }
            foreach (var d in source.GetDirectories())
            {
                var outpath = System.IO.Path.Combine(dest, d.Name);
                if (System.IO.Directory.Exists(outpath) == false)
                    System.IO.Directory.CreateDirectory(outpath);
                CopyFolder(d, outpath);
            }
        }
    }
}
